<template>
<div>
  <h3>发表评论</h3>
  <hr>
  <textarea placeholder="请输入要bb的内容(最多120字）" maxlength="120" v-model="msg"></textarea>
  <mt-button type="primary" size="large" @click="postComment">发表评论</mt-button>
    <div class="cmt-list">
        <div class="cmt-item" v-for="(item,i) in list" :key="i">
            <div class="cmt-title">
                第{{i+1}}楼&nbsp;&nbsp;用户：{{item.user_name}}&nbsp;&nbsp;发表时间:{{item.add_time}}
            </div>
            <div class="cmt-body">
                {{item.content}}
            </div>
        </div>
    </div>
  <mt-button type="danger" size="large" plain @click="more">加载更多</mt-button>
  </div>
</template>

<script>
    export default {
        data(){
            return {
                list: [], //评论数据
                page: 1, //页码
                msg: '' //评论内容
            }
        },
        created() {
            this.getComment()
        },
        methods: {
            getComment(){
                //获取所有评论数据
                this.$http.get('api/getcomments/'+this.id+'?pageindex='+ this.page).then(result=>{
                    if(result.body.status == 0){
                        this.list = this.list.concat(result.body.message)
                    }
                })
            },
            more(){
                //加载更多
                this.page++
                this.getComment()
            },
            postComment(){
                //校验是否为空内容
                if(this.msg.trim() == 0){
                    alert('评论内容不能为空')
                    return;
                }
                //发表评论
                this.$http.post('api/postcomment/'+this.id,{content:this.msg.trim()}).then(result=>{
                    if(result.body.status == 0){
                        //拼接出一个评论对象
                        var cmt = {user_name: '匿名用户',add_time: Date.now(),content:this.msg.trim()}
                        this.list.unshift(cmt)
                        this.msg = ''
                    }
                })
            }
        },
        props: ["id"]
    }
</script>

<style scoped>
.cmt-title {
    background-color: #ccc;
    font-size: 0.8em
}
textarea {
    font-size: 14px;
    height: 85px;
    margin: 0
}
.cmt-list {
    margin-top: 5px
}
h3 {
    font-size: 20px; 
    line-height: 40px;
    font-weight: 700;
    padding: 0;
    margin: 0;
}
.cmt-body {
    height: 36px;
    line-height: 36px;
    text-indent: 2em;
}
</style>

